namespace SimpleWebServices.Web
{
    using System.Web;
    using System.Xml.XPath;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.Mvc;
    using Xunit;

    public sealed class TenantSeeOtherFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<TenantSeeOther>()
                .DerivesFrom<Redirection>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("redirection")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            TenantSeeOther actual = "<redirection location='/path' />".Deserialize<TenantSeeOther>();

            Assert.NotNull(actual);

            Assert.Equal<string>("/path", actual.Location);
        }

        [Fact]
        public void deserializeEmpty()
        {
            TenantSeeOther actual = "<redirection />".Deserialize<TenantSeeOther>();

            Assert.NotNull(actual);

            Assert.Null(actual.Location);
        }

        [Fact]
        public void serialize()
        {
            TenantSeeOther obj = new TenantSeeOther
            {
                Location = "/path"
            };

            IXPathNavigable xml = obj.Serialize();

            string xpath = "1=count(/redirection[@location='/path'])";

            Assert.True((bool)xml.CreateNavigator().Evaluate(xpath));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new TenantSeeOther());
        }

        [Fact]
        public void ctor_string()
        {
            string expected = "/path";
            string actual = new TenantSeeOther(expected).Location;

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void op_ToActionResult()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/-",
                    xml = "<tenant />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/-/sample").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                TenantSeeOther obj = new TenantSeeOther
                {
                    Location = "{tenant-path}/location"
                };

                string expected = "/-/location";
                string actual = (obj.ToActionResult() as SeeOtherResult).Location;

                Assert.Equal<string>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }
    }
}